package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.ModuleDao;
import com.itheima.domain.Module;
import com.itheima.service.ModuleService;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Stream;

public class ModuleServiceImpl implements ModuleService {
    @Override
    public PageInfo<Module> findByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = null;
        PageInfo<Module> info = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            ModuleDao moduleDao = MybatisUtil.getMapper(sqlSession, ModuleDao.class);
            PageHelper.startPage(currentPage,pageSize);
            List<Module> list=moduleDao.findAll();
            info = new PageInfo<>(list);
        } finally {
            MybatisUtil.close(sqlSession);
        }

        return info;
    }

    @Override
    public void save(Module module) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            ModuleDao moduleDao = MybatisUtil.getMapper(sqlSession, ModuleDao.class);
            String id = UUID.randomUUID().toString();
            module.setId(id);
            moduleDao.save(module);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public Module findById(String id) {
        SqlSession sqlSession = null;
        Module module= null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            ModuleDao moduleDao = MybatisUtil.getMapper(sqlSession, ModuleDao.class);
            module = moduleDao.findById(id);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return module;
    }

    @Override
    public void update(Module module) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            ModuleDao moduleDao = MybatisUtil.getMapper(sqlSession, ModuleDao.class);

            moduleDao.update(module);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            ModuleDao moduleDao = MybatisUtil.getMapper(sqlSession, ModuleDao.class);
            Stream.of(ids).forEach(id->moduleDao.delete(id));
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<Module> findAll() {
        SqlSession sqlSession = null;
        List<Module> list = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            ModuleDao moduleDao = MybatisUtil.getMapper(sqlSession, ModuleDao.class);
            list=moduleDao.findAll();

        } finally {
            MybatisUtil.close(sqlSession);
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> findModulesByRoleId(String roleId) {
        SqlSession sqlSession = null;
        List<Map<String, Object>> list= null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            ModuleDao moduleDao = MybatisUtil.getMapper(sqlSession, ModuleDao.class);
            list = moduleDao.findModulesByRoleId(roleId);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return list;
    }
}
